using System;
using System.Collections.Generic;
using System.Reflection;
using BaseAmd.BaseDevice;

namespace BaseAmd.Common
{
	public static class ExtendUtil
	{
		public static DbCompareRetEnum EntityCompare<T>(T src, T dst, List<string> compareNotIncludeCols)
		{
			if (dst == null)
			{
				return DbCompareRetEnum.Add;
			}
			PropertyInfo[] properties = typeof(T).GetProperties();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				if (!(compareNotIncludeCols?.Contains(propertyInfo.Name) ?? false))
				{
					object value = propertyInfo.GetValue(src, null);
					object value2 = propertyInfo.GetValue(dst, null);
					if (value?.ToString() != value2?.ToString())
					{
						return DbCompareRetEnum.Update;
					}
				}
			}
			return DbCompareRetEnum.None;
		}

		public static string GetPkValue<T>(T entity, List<string> pkColList)
		{
			string text = "";
			foreach (string pkCol in pkColList)
			{
				string text2 = typeof(T).GetProperty(pkCol)!.GetValue(entity)!.ToString();
				text = text + pkCol + ":" + text2;
			}
			return text;
		}

		public static T QueryFirst<T>(List<T> list, string queryValue, List<string> pkColList)
		{
			if (list == null)
			{
				throw new ArgumentNullException("list");
			}
			if (pkColList == null)
			{
				throw new ArgumentNullException("pkColList");
			}
			foreach (T item in list)
			{
				string pkValue = ExtendUtil.GetPkValue(item, pkColList);
				if (pkValue == queryValue)
				{
					return item;
				}
			}
			return default(T);
		}
	}
}
